package com.view.jurisdictionmanage;

import com.bean.Power;
import com.bean.Role;
import com.bean.User;
import com.dao.UserDao;
import com.lovo.netCRM.component.*;
import com.service.PowerService;
import com.service.RoleService;
import com.service.UserService;
import com.service.impl.PowerServiceImpl;
import com.service.impl.RoleServiceImpl;
import com.service.impl.UserServiceImpl;
import com.sun.tools.javac.Main;
import com.util.MyCheckBox;
import com.view.MainFrame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;


public class UserAddDialog extends JDialog{

    private LovoButton addBtn;
    private LovoButton cancelBtn;
    private LovoTxt nameTxt,pwdTxt,realTxt;
    private MyCheckBox roleCbx;
    private Checkbox checkbox;
    private JLabel powerLeb;

    private MainFrame frame ;
    private UserService userService = new UserServiceImpl();
    private RoleService roleService = new RoleServiceImpl();
    private PowerService powerService = new PowerServiceImpl();

    public UserAddDialog(MainFrame frame) {
        // TODO Auto-generated constructor stub
        super(frame,"添加新用户",true);
        this.frame = frame;
        this.setBounds(350, 250, 520, 400);
        this.setLayout(null);
        cbxFlush();
        this.init();
        this.setVisible(true);
    }

    public void cbxFlush(){
        String[] strs = roleService.getAllRoles();
        roleCbx = new MyCheckBox("角色",strs,20,100,this){
            @Override
            public void addListener(){
                    String[] strings = roleCbx.getItem();
                    List<String> stringList = new ArrayList<>();
                    stringList = Arrays.asList(strings.clone());
                    Set<String> powers = new HashSet<>();
                    for (String str:stringList) {
                        List<Power> powerList = new ArrayList<>();
                        powerList = powerService.getPower(str);
                        System.out.println(powerList);
                        for (Power power:powerList) {
                            String powerName = power.getPowerName();
                            powers.add(powerName);
                        }
                    }
                    powerLeb.setText("<html>拥有权限:"+powers+"</html>");
            }
        };
    };


    private void init() {
        // TODO Auto-generated method stub
        nameTxt = new LovoTxt("用户名",20,20,this);
        pwdTxt = new LovoTxt("密码",250,20,this);
        realTxt = new LovoTxt("真实姓名",20,50,this);


        powerLeb = new JLabel();
        powerLeb.setBounds(50,200,350,150);
        powerLeb.setText("拥有权限:");
        this.add(powerLeb);

        this.addBtn = new LovoButton("添加", 120, 320, this);
        this.cancelBtn = new LovoButton("取消", 270, 320, this);

        addBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                User user = new User();
                String name = nameTxt.getText();
                String password = pwdTxt.getText();
                String realName = realTxt.getText();
                user.setRealName(realName);
                user.setAccount(name);
                user.setPassword(password);
                String[] strings = roleCbx.getItem();
                List<String> rolenames = Arrays.asList(strings);
                System.out.println(rolenames.toString());
                if(userService.addUser(user,rolenames)){
                    JOptionPane.showMessageDialog(null,"添加成功");
                    UserAddDialog.this.dispose();
                    frame.flush();
                }else {
                    JOptionPane.showMessageDialog(null,"添加失败");
                }
            }
        });

    }

}
